/*
 * Class to interact with Login Dialog
 * 
 */
package acc;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;

/**
 *
 * @author Haitm
 */
public class LoginInteraction {

    // Read User Information to a vector, use to check when an user gets login
    public static Vector readUser() {
        String _sql = "select [User].UserID, [User].UserName, [Group].GroupName, [Group].Permission From [User] join [Group]  "
                + " on [User].GroupID = [Group].GroupID  ";
        Vector vt = new Vector();
        ResultSet rs = DataInteraction.createResultSet(_sql);
        try {
            while (rs.next()) {
                Vector temp = new Vector();
                temp.add(rs.getString("UserID"));
                temp.add(rs.getString("UserName"));
                temp.add(rs.getString("GroupName"));

                vt.add(temp);
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        } finally {
            return vt;
        }
    }

    public static Vector getUser() {
        String _sql = "select [User].UserID, [User].UserName, [User].Password, [User].FirstName, [User].LastName, [User].Sex, [User].Birthday, [User].Email, [User].Phone, [User].Address, [User].Status, [Group].GroupID, [Group].GroupName, [Group].Permission "
                + "from [User] join [Group]"
                + " on [User].GroupID = [Group].GroupID";
        Vector vt = new Vector();
        ResultSet rs = DataInteraction.createResultSet(_sql);
        try {
            while (rs.next()) {
                Vector temp = new Vector();
                temp.add(rs.getString("UserID"));
                temp.add(rs.getString("UserName"));
                temp.add(rs.getString("Password"));
                temp.add(rs.getString("FirstName"));
                temp.add(rs.getString("LastName"));
                temp.add(rs.getString("Sex"));
                temp.add(Validation.convertDateToString(rs.getDate("Birthday")));
                temp.add(rs.getString("Email"));
                temp.add(rs.getString("Phone"));
                temp.add(rs.getString("Address"));
                temp.add(rs.getString("GroupID"));
                temp.add(rs.getString("GroupName"));
                temp.add(rs.getString("Permission"));
                temp.add(rs.getString("Status"));
                vt.add(temp);
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        } finally {
            return vt;
        }
    }

    // Return a vector with all information of Users
    public static Vector getUserInformation() {
        String _sql = "select [User].UserID, UserName, Password, FullName, Gender, DateOfBirth, "
                + "Address, Email,  [Group].GroupID, GroupName "
                + " From [User] join Permission  on [User].UserID=Permission.UserID join [Group] "
                + " on Permission.GroupID=[Group].GroupID";
        Vector vt = new Vector();
        ResultSet rs = DataInteraction.createResultSet(_sql);
        try {
            while (rs.next()) {
                Vector temp = new Vector();
                temp.add(rs.getString("UserID"));
                temp.add(rs.getString("UserName"));
                temp.add(rs.getString("Password"));
                temp.add(rs.getString("FullName"));
                temp.add(rs.getString("Gender"));
                temp.add(Validation.convertDateToString(rs.getDate("DateOfBirth")));
                temp.add(rs.getString("Address"));
                temp.add(rs.getString("Email"));
                temp.add(rs.getString("GroupID"));
                temp.add(rs.getString("GroupName"));

                vt.add(temp);
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        } finally {
            return vt;
        }
    }
}
